package enahce;

import java.nio.charset.Charset;
import java.util.*;

/**
 * Describe
 *
 * @author maguangming
 * @since 2022/9/16 21:10
 */
public class SwitchDemo {

    public static void main(String[] args) {

        System.out.println(Charset.defaultCharset());
        System.out.println(System.getProperty("file.encoding"));
        System.out.println("中文");

        castWithArrows(1);
        castWithArrows(-1);
        System.out.println("----------");

        castAllowNull("一");
        castAllowNull("xxx");
        castAllowNull(null);
        System.out.println("----------");

        castWithSmartCasting("xxx");

        System.out.println("----------");

        selectorExpression("hello");
        final List<String> list = Arrays.asList("hello", "world", "!");
        // final List<String> list = List.of("hello", "world", "!");
        list.set(2, "~");
        selectorExpression(list);
        System.out.println(list);
        selectorExpression(Map.of("hello", "world"));
        try {
            selectorExpression(new Object());
        } catch (IllegalStateException e) {
            System.out.println("IllegalStateException:" + e.getLocalizedMessage());
        }

        System.out.println("----------");


    }

    //箭头函数
    public static void castWithArrows(int i) {
        switch (i) {
            case 1 -> System.out.println("一");
            case 2 -> System.out.println("二");
            case 3 -> System.out.println("三");
            case -1, -2 -> System.out.println("-1, -2");
            default -> System.out.println("默认");
        }
    }

    // 允许null
    public static void castAllowNull(String str) {
        switch (str) {
            case "一" -> System.out.println("1");
            case "二" -> System.out.println("2");
            case "三" -> System.out.println("3");
            case null -> System.out.println("null");
            default -> System.out.println("默认");
        }
    }

    //智能型转
    public static void castWithSmartCasting(Object obj) {
        if (obj instanceof String str) {
            System.out.println(str);
            switch (str) {
                case "一" -> System.out.println("1");
                case "二" -> System.out.println("2");
                case "三" -> System.out.println("3");
                default -> System.out.println("默认");
            }
        }
    }

    public static void selectorExpression(Object obj) {
        switch (obj) {
            case String str -> System.out.println("str");
            case List list -> System.out.println("list");
            case Map map -> System.out.println("map");
            case Set set -> System.out.println("set");
            case null -> System.out.println("null");
            default -> throw new IllegalStateException("Unexpected value: " + obj);
        }
    }

    sealed interface FunA permits FunAImpl {
        void a();
    }
}

